﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Trock.Learning.SyncFunc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("你好");
            //Task<int> sum = GetValue2(5000);
            //Thread.Sleep(300);
            //Console.WriteLine("sum=");
            //Console.ReadKey();

            Console.WriteLine(string.Format("{0} 创建Producer", DateTime.Now.ToString("HH:mm:ss.fff")));
            Producer p = new Producer();
            for (int i = 0; i < 20; i++)
            {
                p.Trigger();
                Thread.Sleep(100);
            }
            //Console.WriteLine(string.Format("{0} 获取数据", DateTime.Now.ToString("HH:mm:ss.fff")));
            //int value = 0;
            //if (!p.GetValue(ref value))
            //    Console.WriteLine(string.Format("{0} 获取超时", DateTime.Now.ToString("HH:mm:ss.fff")));
            //else
            //    Console.WriteLine(string.Format("{0} 数据:" + value, DateTime.Now.ToString("HH:mm:ss.fff")));
            //Console.WriteLine(string.Format("{0} Done:", DateTime.Now.ToString("HH:mm:ss.fff")));
            Console.ReadKey();
        }

        static async Task<int> GetValue(int num)
        {
            return await Task.Factory.StartNew<int>(() =>
             {
                 for (int i = 0; i < 20; i++)
                 {
                     Console.WriteLine("www");
                     Thread.Sleep(100);
                 }
                 int sum = 0;
                 for (int i = 0; i < num; i++)
                     sum += i;
                 Console.WriteLine("s=" + sum);
                 return sum;
             });
        }

        static Task<int> GetValue2(int num)
        {
            return Task.Factory.StartNew<int>(() =>
            {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("www");
                    Thread.Sleep(100);
                }
                int sum = 0;
                for (int i = 0; i < num; i++)
                    sum += i;
                Console.WriteLine("s=" + sum);
                return sum;
            });
        }
    }
}
